package org.ielse.frame.t.tz.parallax;

import java.util.ArrayList;
import java.util.List;

import org.ielse.frame.R;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.nineoldandroids.view.ViewHelper;

public class ParallaxContainer extends FrameLayout {

	public static final String LAYOUT_ID = "layout_id";

	private int mWidth;

	private final List<ParallaxFragment> fragments = new ArrayList<ParallaxFragment>();

	public ParallaxContainer(Context context) {
		this(context, null);
	}

	public ParallaxContainer(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void setUp(int... layoutIds) {
		for (int i = 0; i < layoutIds.length; i++) {
			ParallaxFragment f = new ParallaxFragment();
			Bundle args = new Bundle();
			args.putInt(LAYOUT_ID, layoutIds[i]);
			f.setArguments(args);
			fragments.add(f);
		}

		FragmentActivity activity = (FragmentActivity) getContext();
		ParallaxAdapter adapter = new ParallaxAdapter(activity.getSupportFragmentManager(), fragments);

		ViewPager viewPager = new ViewPager(getContext());
		viewPager.setId(R.id.parallax_pager);
		viewPager.setAdapter(adapter);

		addView(viewPager);
		viewPager.setOnPageChangeListener(onPageChangeListener);
	}

	@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		mWidth = w;
	}

	private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {
		@Override public void onPageSelected(int position) {
		}

		@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			ParallaxFragment selfFragment = fragments.get(position);
			ParallaxFragment nextFragment = position < fragments.size() - 1 ? fragments.get(position + 1) : null;

			List<View> selfViews = selfFragment.getParallaxViews();
			for (View view : selfViews) {
				ParallaxViewTag tag = (ParallaxViewTag) view.getTag(R.id.parallax_view_tag);
				if (tag != null) {
					if (tag.alpha != 0) ViewHelper.setAlpha(view, 1 - positionOffset * tag.alpha);
					if (tag.translationX != 0) ViewHelper.setTranslationX(view, positionOffsetPixels * tag.translationX);
					if (tag.translationY != 0) ViewHelper.setTranslationY(view, positionOffsetPixels * tag.translationY);
					if (tag.scaleX != 0) ViewHelper.setScaleX(view, 1 - positionOffset * tag.scaleX);
					if (tag.scaleY != 0) ViewHelper.setScaleY(view, 1 - positionOffset * tag.scaleY);
					if (tag.rotation != 0) ViewHelper.setRotation(view, positionOffset * tag.rotation);
				}
			}

			if (nextFragment != null) {
				List<View> nextViews = nextFragment.getParallaxViews();
				for (View view : nextViews) {
					ParallaxViewTag tag = (ParallaxViewTag) view.getTag(R.id.parallax_view_tag);
					if (tag != null) {
						if (tag.alpha != 0) ViewHelper.setAlpha(view, positionOffset * tag.alpha);
						if (tag.translationX != 0) ViewHelper.setTranslationX(view, (positionOffsetPixels - mWidth) * tag.translationX);
						if (tag.translationY != 0) ViewHelper.setTranslationY(view, (positionOffsetPixels - mWidth) * tag.translationY);
						if (tag.scaleX != 0) ViewHelper.setScaleX(view, 1 - (1 - positionOffset) * tag.scaleX);
						if (tag.scaleY != 0) ViewHelper.setScaleY(view, 1 - (1 - positionOffset) * tag.scaleY);
						if (tag.rotation != 0) ViewHelper.setRotation(view, positionOffset * tag.rotation);
					}
				}
			}
		}

		@Override public void onPageScrollStateChanged(int state) {

		}
	};

}
